package dwei.textwrap.wrapper
{
	import flash.display.DisplayObject;
	
	import mx.core.IInvalidating;
	import mx.core.UIComponent;
	
	import dwei.core.load.LoadMgr;
	import dwei.core.load.LoadType;
	import dwei.core.load.LoadVO;
	import dwei.textwrap.WrapperAlign;

	/**
	 * 图形包装器
	 */	
	public class WrapperGraphics extends Wrapper
	{
//============================================================================
//PROPERTY
//============================================================================
		private var _sourceChanged:Boolean = false;
		protected var _source:Object;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function WrapperGraphics()
		{
			super();
		}
//============================================================================
//OVERRIDE
//============================================================================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if (_sourceChanged)
			{
				if (_source is String)
					LoadMgr.add(String(_source), LoadType.DISPLAYABLE, contentLoaded_cb, null, contentLoadError_cb);
				else
				{
					removeAllChildren();
					if (_source is DisplayObject) addChild(_source as DisplayObject);
					invalidateSize();
				}
				_sourceChanged = false;
			}
		}
		override protected function measure():void
		{
			super.measure();
			measuredWidth = measuredHeight = 0;
			if (numChildren == 0) return;
			var child:DisplayObject = getChildAt(0);
			measuredWidth = child.width;
			measuredHeight = child.height;
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if (numChildren == 0) return;
			var child:DisplayObject = getChildAt(0);
			switch (_verticalAlign)
			{
				case WrapperAlign.TOP:
					child.y = 0;
					break;
				case WrapperAlign.MIDDLE:
					child.y = (unscaledHeight - child.height) / 2 - 2;
					break;
				default:
					child.y = unscaledHeight - 2 - child.height;
					break;
			}
		}
//============================================================================
//PRIVATE
//============================================================================
		public function removeAllChildren():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		}
//============================================================================
//CALLBACK
//============================================================================
		private function contentLoaded_cb(vo:LoadVO):void
		{
			removeAllChildren();
			if (vo.content != null) addChild(vo.content);
			invalidateSize();
		}
		private function contentLoadError_cb(vo:LoadVO):void
		{
			trace(vo.content);
		}
//============================================================================
//GETTER/SETTER
//============================================================================
		public function get source():Object
		{
			return _source;
		}

		public function set source(value:Object):void
		{
			if (value == source) return;
			_source = value;
			_sourceChanged = true;
			invalidateProperties();
		}

	}
}